<template>
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form ref="form" :inline="true" :model="form" label-width="130px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="会员手机号">
              <el-input v-model="form.username" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="昵称">
              <el-input v-model="form.nickName" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="会员姓名">
              <el-input v-model="form.realname" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="推荐人手机号">
              <el-input v-model="form.refereePhone" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="注册时间">
              <el-date-picker
                v-model="dates"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="['00:00:00', '23:59:59']"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="账号状态">
              <el-select v-model="form.status" placeholder="请选择账号状态">
                <el-option
                  v-for="item in statusOption"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="会员类型">
              <el-select v-model="form.accountType" placeholder="请选择会员类型">
                <el-option
                  v-for="item in accountTypeOption"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="账号类型">
              <el-select v-model="form.isJrAccount" placeholder="请选择账号类型">
                <el-option
                  v-for="item in isJrAccountOption"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-button style="margin-left:60px" type="primary" @click="searchFn">查询</el-button>
            <el-button
              v-if="hasPermission('AdminUserProvider.save')"
              type="primary"
              @click="toRoutes('addVip')"
            >新增</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div v-loading="tableLoading" class="listDiv">
      <!-- table列表 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column fixed prop="userId" label="用户ID" />
        <el-table-column prop="nickName" label="会员昵称" />
        <el-table-column prop="businessAccountType" label="会员类型" />
        <el-table-column prop="realname" label="会员姓名" />
        <el-table-column prop="username" label="会员手机号" width="120">
          <template slot-scope="scope">
            <div>{{scope.row.username | phonereg }}</div>
          </template>
        </el-table-column>
        <el-table-column label="证件类型">
          <template slot-scope="scope">
            <div v-for="(e,index) in scope.row.identityType" :key="index">{{ e | cardType }}</div>
          </template>
        </el-table-column>
        <el-table-column label="证件号码">
          <template slot-scope="scope">
            <div v-for="(e,index) in scope.row.identity" :key="index">{{ e }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="refereeName" label="推荐人姓名" />
        <el-table-column prop="referee" label="推荐人手机号" width="120">
          <template slot-scope="scope">
            <div>{{scope.row.referee | phonereg }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="createdAt" label="注册时间" />
        <!-- <el-table-column fixed="right" width="80" align="center" label="账号状态">
                    <template slot-scope="scope">
                        <el-switch
                            class="switchStyle"
                            v-model="scope.row.status"
                            @click.native="swithFn(scope.row.userId,scope.row.status)"
                            active-color="#13ce66"
                            inactive-color="#eee"
                            :active-value="0"
                            :inactive-value="1"
                        ></el-switch>
                    </template>
        </el-table-column>-->
        <el-table-column fixed="right" width="80" align="center" label="账号状态">
          <template slot-scope="scope">
            <el-switch
              class="switchStyle switchStyleOpa"
              disabled
              v-model="scope.row.status"
              @click.native="clickStatus(scope.row.userId,scope.row.status,'changeAccountStatus')"
              active-color="#13ce66"
              inactive-color="#eee"
              :active-value="0"
              :inactive-value="1"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed="right" width="80" align="center" label="积分状态">
          <template slot-scope="scope">
            <el-switch
              class="switchStyle switchStyleOpa"
              disabled
              v-model="scope.row.accountStatus"
              @click.native="clickStatus(scope.row.userId,scope.row.accountStatus,'changeIntegralStatus')"
              active-color="#13ce66"
              inactive-color="#eee"
              :active-value="0"
              :inactive-value="1"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed="right" width="120" align="center" label="账号类型">
          <template slot-scope="scope">
            <div>
              <p v-if="scope.row.isJrAccount ==1">金融账户</p>
              <p v-if="scope.row.isJrAccount =='0'">普通账户</p>
            </div>
          </template>
        </el-table-column>
        <el-table-column fixed="right" prop="superCode" label="推荐码" />
        <el-table-column fixed="right" width="120" align="center" label="备注">
          <template slot-scope="scope">
            <div>
              <p>账号：{{scope.row.accountStatusRemark }}</p>
              <p>积分：{{scope.row.integralStatusRemark }}</p>
            </div>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="200">
          <template slot-scope="scope">
            <el-button
              v-if="hasPermission('AdminUserProvider.update')"
              @click="toRoutes('editVip',scope.row.userId)"
              type="text"
              size="small"
            >编辑</el-button>
            <el-button
              v-if="hasPermission('AdminUserProvider.get')"
              @click="toRoutes('vipInfo',scope.row.userId,scope.row.businessAccountType)"
              type="text"
              size="small"
            >详情</el-button>
            <el-button
              v-if="hasPermission('AdminUserServerProvider.saveUserServer')"
              type="text"
              size="small"
              @click="toRoutes('joinFacilitator',scope.row.userId,scope.row.businessAccountType)"
            >加入美学设计师</el-button>
            <!-- v1.6 -->
            <el-button
              type="text"
              size="small"
              v-if="hasPermission('AdminUserProvider.replacePhone')"
              @click="changePhone(scope.row)"
            >更换手机号</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 冻结解冻原因 -->
      <el-dialog
        :title="dialogsTitle"
        :visible.sync="dialogs"
        :before-close="handleClose"
        width="468px"
      >
        <el-form ref="freezeForm" :inline="true" label-width="140px">
          <el-form-item :label="dialogsTitle">
            <el-input v-model="freezeForm.freeze" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose()">取 消</el-button>
          <el-button type="primary" @click="submitBtns()">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="form.pageNum"
            :page-sizes="page_sizes"
            :page-size="form.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
          ></el-pagination>
        </div>
      </div>
    </div>
    <el-dialog title="更换手机号" :visible.sync="dialogVisible" width="30%">
      <el-form :inline="true" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="原手机号">{{params.username}}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="变更手机号">
              <el-input v-model="params.phone"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="sure">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
 
<script>
import {
  getSearchList,
  updateStatus,
  changeAccountStatus,
  changeIntegralStatus,
  replacePhone
} from "@/api/vipIndex";
export default {
  name: "VipIndex",
  filters: {
    cardType: function(e) {
      return window.helper.cardType(String(e));
    }
  },
  data() {
    return {
      dialogVisible: false,
      params: {},
      apis: null,
      dialogsTitle: null,
      freezeFormStatus: null,
      dialogs: false,
      freezeForm: {
        freeze: null
      },
      tableLoading: true,
      dates: [],
      form: {
        username: "",
        nickName: "",
        realname: "",
        refereePhone: "",
        status: "-1",
        accountType: "all",
        isJrAccount: -1,
        createdAtStart: null,
        createdAtEnd: null,
        pageNum: 1,
        pageSize: 10
      },
      statusOption: [
        {
          value: "-1",
          label: "全部"
        },
        {
          value: "0",
          label: "启用"
        },
        {
          value: "1",
          label: "停用"
        }
      ],
      accountTypeOption: [
        {
          value: "all",
          label: "全部"
        },
        {
          value: "ordinary",
          label: "普通会员"
        },
        {
          value: "369",
          label: "369Plus会员"
        }
      ],
      isJrAccountOption: [
        {
          value: -1,
          label: "全部"
        },
        {
          value: 0,
          label: "普通账号"
        },
        {
          value: 1,
          label: "金融账号"
        }
      ],
      tableData: [],
      page_sizes: [10, 30, 50, 100],
      page_totals: 60
    };
  },
  inject: ["reload"],
  mounted() {
    // this.getList(this.form);
    this.getQueryString("username");
  },
  methods: {
    //手机号
    changePhone(datas) {
      console.log(datas);
      this.params = datas;
      this.params["type"] = 10;
      this.dialogVisible = true;
    },
    async sure() {
      let res = await replacePhone(this.params);
      if (res.data.code == 0) {
        this.$message.success(res.data.msg);
        this.dialogVisible = false;
        this.getList(this.form);
      } else {
        this.$message.warning(res.data.msg);
      }
    },
    // 数据权限按钮
    hasPermission(e) {
      return window.helper.hasPermission(e);
    },
    // 获取url参数
    getQueryString(name) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
      var r = window.location.search.substr(1).match(reg);
      if (r && r.length >= 3 && /^1[3456789]\d{9}$/.test(unescape(r[2]))) {
        var phone = unescape(r[2]);
        this.form.username = phone;
        this.searchFn();
      } else {
        this.getList(this.form);
      }
    },
    // 成功状态弹窗
    succStatus(txt) {
      this.$message({
        message: txt + "成功",
        type: "success"
      });
    },
    // 失败状态弹窗
    errorStatus(txt) {
      this.$message.error(txt + "失败");
    },
    // 列表数据获取
    getList(e) {
      this.tableLoading = true;
      const datas = { ...e };
      datas.status = String(datas.status);
      getSearchList(datas)
        .then(res => {
          // console.log(res)
          if (!res.data.code) {
            // console.log(res)
            this.tableData = res.data.data.list;
            this.page_totals = res.data.data.count;
          } else {
            this.$message.error(res.data.msg);
          }
          this.tableLoading = false;
        })
        .catch(error => {
          this.$message.error(error);
        });
    },
    // 查询
    searchFn() {
      this.form.pageNum = 1;
      if (this.dates != null) {
        if (this.dates.length) {
          this.form.createdAtStart = this.dates[0];
          this.form.createdAtEnd = this.dates[1];
        }
      } else {
        this.form.createdAtStart = null;
        this.form.createdAtEnd = null;
      }
      const datas = { ...this.form };

      datas.status = Number(datas.status);
      console.log(datas);
      this.getList(datas);
      // console.log(JSON.stringify(this.form))
    },
    // 修改用户账户状态（正常-冻结）
    changeAccountStatus(datas) {
      changeAccountStatus(datas)
        .then(res => {
          if (!res.data.code) {
            this.succStatus("修改");
            this.getList(this.form);
          } else {
            this.$message.error(res.data.msg);
          }
          this.reload();
        })
        .catch(error => {
          this.$message.error(error);
          this.reload();
        });
    },
    // 修改用户积分状态（正常-冻结）
    changeIntegralStatus(datas) {
      changeIntegralStatus(datas)
        .then(res => {
          if (!res.data.code) {
            this.succStatus("修改");
            this.getList(this.form);
          } else {
            this.$message.error(res.data.msg);
          }
          this.reload();
        })
        .catch(error => {
          this.$message.error(error);
          this.reload();
        });
    },
    // 路由跳转
    toRoutes(e, userId, type) {
      const route = { name: e, query: { userId: userId, type: type } };
      this.$router.push(route);
    },
    // 改变页面条数
    handleSizeChange(val) {
      this.form.pageSize = val;
      // console.log(this.form);
      this.getList(this.form);
    },
    // 选择页数
    handleCurrentChange(val) {
      this.form.pageNum = val;
      // console.log(this.form);
      this.getList(this.form);
    },
    // switch开关
    clickStatus(userId, status, apis) {
      this.apis = apis;
      this.dialogs = true;
      this.freezeForm["userId"] = userId;
      this.freezeFormStatus = status;
      if (status == 1) {
        this.dialogsTitle = "解冻原因";
      } else {
        this.dialogsTitle = "冻结原因";
      }
    },
    // 关闭冻结解冻弹窗
    handleClose() {
      this.dialogs = false;
    },
    // 弹窗提交
    submitBtns() {
      if (this.freezeForm.freeze) {
        if (this.freezeFormStatus == 0) {
          this.freezeFormStatus = 1;
        } else {
          this.freezeFormStatus = 0;
        }
        if (this.apis === "changeAccountStatus") {
          this.freezeForm["status"] = this.freezeFormStatus;
          this.freezeForm["accountStatusRemark"] = this.freezeForm.freeze;
          this.changeAccountStatus(this.freezeForm);
        } else {
          this.freezeForm["accountStatus"] = this.freezeFormStatus;
          this.freezeForm["integralStatusRemark"] = this.freezeForm.freeze;
          this.changeIntegralStatus(this.freezeForm);
        }
        this.dialogs = false;
        this.freezeForm = { freeze: null };
      } else {
        if (this.dialogsTitle == "解冻原因") {
          this.$message.error("请输入解冻原因，解冻原因为必填项");
        } else {
          this.$message.error("请输入冻结原因，冻结原因为必填项");
        }
      }
    }
  }
};
</script>
<style scoped>
/* 分页 */
.pagesCon {
  height: 40px;
  margin-top: 20px;
}
.pagesCon > div {
  float: right;
}
/* 表单样式修改 */
.el-range-editor--medium.el-input__inner {
  width: 236px;
}
</style>
